<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新建店铺</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/addStore.css">
    <link rel="stylesheet" href="css/registerCommon.css">
</head>
<body>
<div class="nav">电子物品交易</div>
<div class="container">
    <!-- 登录对话框 -->
    <div class="dialog">
        <h2>新建店铺</h2>

        <div class="row">
            <span>店铺名称</span>
            <input type="text" id="storeName" placeholder="店铺名称">
        </div>

        <div class="row">
            <span>店铺简介</span>
            <input type="text" id="storeIntroduction" placeholder="请输入简介">
        </div>
        <div class="row">
            <span>联系方式</span>
            <input type="number" id="contactInfo" placeholder="请输入联系方式">
        </div>
        <div class="row">
            <span>验证码</span>
            <input name="verifyCode" id="verifyCode" placeholder="请输入验证码" type="text" >
        </div>
        <div class="row">
            <img title="单击图片刷新！" id="imgCode" src="/util/verifycode" onclick="this.src='/util/verifycode'"/>
        </div>
        <div class="row">
            <button class="submit" onclick="addStore()">提交</button>
        </div>

    </div>

</div>

<script src="js/jquery.min.js"></script>
<script>
    let userId = localStorage.getItem("userId");
    if(userId === null || userId === "") {
        alert("请先登录");
        location.href = "/login.html";
    }
    function addStore() {
        //校验参数
        let pass = true;
        $(' input').each(function () {
            let inputValue = $(this).val();
            if (inputValue === null || inputValue === "") {
                const labelText = $(this).prev('span').text();
                pass = false;
                alert("请输入" + labelText);
                return false;
            }
        });
        if (!pass) {
            return;
        }
        $.ajax({
            type: 'post',
            url: '/store/addStore',
            data: {
                userId: userId,
                storeName: $("#storeName").val(),
                storeIntroduction: $("#storeIntroduction").val(),
                contactInfo: $("#contactInfo").val(),
                verifyCode: $("#verifyCode").val()
            }
            ,success: function(result) {
                if(result.code === "SUCCESS") {
                    alert("添加成功");
                    history.back();
                }else{
                    alert(result.errMsg);
                    $("#imgCode").click();
                }
            }
            ,error: function(error) {
                if (error != null && error.status === 401) {
                    alert("请先登录");
                    location.href = "/login.html";
                } else {
                    alert("内部错误");
                }
            }
        });
    }
</script>
</body>
</html>